<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Management Console - CarbonTrack</title>
  <link rel="icon" href="/img/team.jpg" type="image/JPG">
  <!-- 引入Bootstrap和jQuery的CDN链接 -->
  <link rel="stylesheet" href="./css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
  <script src="js/jquery.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/utils.js"></script>
  <script src="js/nav.js"></script>
  <!-- SF Pro 字体 -->
  <link href="https://fonts.cdnfonts.com/css/sf-pro-display" rel="stylesheet">
  <!-- Intersection Observer polyfill -->
  <script src="https://cdn.jsdelivr.net/npm/intersection-observer@0.12.2/intersection-observer.js"></script>
  <!-- iOS设计CSS -->
  <link rel="stylesheet" href="./css/ios-design.css">
  <link rel="stylesheet" href="./css/index.css">
  <style>
    /* Reuse styles from ios-design.css and index.css where possible */
    body {
      transition: background-color 0.3s ease, color 0.3s ease;
    }
    
    /* Admin Header Styles */
    .admin-header {
      background: linear-gradient(135deg, var(--ios-blue), var(--ios-purple));
      color: white;
      padding: 50px 0 40px;
      text-align: center;
      border-radius: 0 0 20px 20px;
      margin: 0 0 40px 0;
      position: relative;
      overflow: hidden;
    }

    .admin-header::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="25" cy="25" r="2" fill="rgba(255,255,255,0.1)"/><circle cx="75" cy="75" r="1.5" fill="rgba(255,255,255,0.1)"/><circle cx="85" cy="25" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="15" cy="85" r="1.5" fill="rgba(255,255,255,0.1)"/></svg>');
      pointer-events: none;
    }
    
    .admin-header h1 {
      font-weight: 800;
      margin-bottom: 20px;
      color: white;
      font-size: 3rem;
      letter-spacing: -0.025em;
      position: relative;
      z-index: 1;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
      animation: fadeInUp 0.8s ease-out;
    }
    
        .admin-header p {
      opacity: 0.95;
      max-width: 600px;
      margin: 0 auto;
      font-size: 1.15rem;
      color: white;
      position: relative;
      z-index: 1;
      line-height: 1.5;
    }
    
    /* Admin Card Styles */
    .admin-cards-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
      gap: 24px;
      padding: 0 8px;
      max-width: 1200px;
      margin: 0 auto;
    }

    .admin-card {
      background: var(--card-bg);
      border-radius: 20px;
      border: 1px solid var(--border-color);
      padding: 32px 24px;
      text-align: center;
      transition: all 0.4s cubic-bezier(0.23, 1, 0.32, 1);
      position: relative;
      overflow: hidden;
      box-shadow: 0 8px 32px rgba(0,0,0,0.06);
      backdrop-filter: blur(10px);
      -webkit-backdrop-filter: blur(10px);
      animation: slideInCard 0.6s ease-out;
      animation-fill-mode: both;
    }

    .admin-card:nth-child(1) { animation-delay: 0.1s; }
    .admin-card:nth-child(2) { animation-delay: 0.2s; }
    .admin-card:nth-child(3) { animation-delay: 0.3s; }
    .admin-card:nth-child(4) { animation-delay: 0.4s; }
    .admin-card:nth-child(5) { animation-delay: 0.5s; }
    .admin-card:nth-child(6) { animation-delay: 0.6s; }

    .admin-card::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: 4px;
      background: linear-gradient(90deg, var(--ios-blue), var(--ios-purple));
      opacity: 0;
      transition: opacity 0.3s ease;
    }

    .admin-card:hover {
      transform: translateY(-12px) scale(1.02);
      box-shadow: 0 20px 60px rgba(0,0,0,0.12);
      border-color: var(--ios-blue);
    }

    .admin-card:hover::before {
      opacity: 1;
    }    .admin-card-body {
      padding: 24px;
      text-align: center;
    }
    
    .admin-card-icon {
      font-size: 3.5rem;
      margin-bottom: 20px;
      background: linear-gradient(135deg, var(--ios-blue), var(--ios-purple));
      background-clip: text;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
    }
    
    .admin-card-title {
      font-weight: 700;
      margin-bottom: 16px;
      font-size: 1.4rem;
      color: var(--ios-label);
      letter-spacing: -0.01em;
    }
    
    .admin-card-text {
      color: var(--ios-secondary-label);
      margin-bottom: 24px;
      font-size: 0.95rem;
      line-height: 1.5;
      opacity: 0.8;
    }
    
    .admin-card-link {
      display: inline-block;
      background: linear-gradient(135deg, var(--ios-blue), var(--ios-indigo));
      color: white;
      border: none;
      border-radius: 14px;
      padding: 16px 32px;
      font-weight: 600;
      font-size: 0.95rem;
      letter-spacing: -0.005em;
      transition: all 0.35s var(--animation-timing-apple);
      text-decoration: none;
      box-shadow: 
        0 6px 20px rgba(0, 122, 255, 0.15),
        0 2px 8px rgba(0, 0, 0, 0.08);
      position: relative;
      overflow: hidden;
      cursor: pointer;
      min-width: 140px;
      text-align: center;
    }

    .admin-card-link::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
      transition: left 0.5s ease;
    }
    
    .admin-card-link:hover {
      transform: translateY(-3px) scale(1.02);
      box-shadow: 
        0 12px 35px rgba(0, 122, 255, 0.3),
        0 4px 15px rgba(0, 0, 0, 0.12);
      color: white;
      background: linear-gradient(135deg, #0066cc, #4c6ef5);
    }

    .admin-card-link:hover::before {
      left: 100%;
    }

    .admin-card-link:active {
      transform: translateY(-1px) scale(0.98);
      box-shadow: 
        0 6px 20px rgba(0, 122, 255, 0.2),
        0 2px 8px rgba(0, 0, 0, 0.1);
      transition: all 0.15s ease;
    }

    .admin-card-link:focus {
      outline: none;
      box-shadow: 
        0 6px 20px rgba(0, 122, 255, 0.15),
        0 0 0 3px rgba(0, 122, 255, 0.1);
    }

    /* Admin Navigation Bar Styles */
    .admin-nav {
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(20px);
      -webkit-backdrop-filter: blur(20px);
      box-shadow: 0 1px 0 rgba(0, 0, 0, 0.05);
      position: sticky;
      top: 0; /* Stick to very top, no offset needed */
      z-index: 1020; /* Above main navbar */
      padding: 8px 0;
      border-bottom: none;
      margin-bottom: 0;
      transition: all 0.3s ease;
    }

    .admin-nav-list {
      display: flex;
      list-style: none;
      margin: 0;
      padding: 0;
      justify-content: center;
      flex-wrap: wrap;
      gap: 4px;
    }

    .admin-nav-item {
      position: relative;
    }

    .admin-nav-link {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 8px 16px;
      color: var(--ios-secondary-label);
      text-decoration: none;
      transition: all 0.2s ease;
      border-radius: 8px;
      font-weight: 500;
      font-size: 13px;
    }

    .admin-nav-link i {
      font-size: 18px;
      margin-bottom: 2px;
    }

    .admin-nav-link:hover {
      color: var(--ios-blue);
      background-color: rgba(0, 122, 255, 0.1);
      text-decoration: none;
    }

    .admin-nav-item.active .admin-nav-link {
      color: var(--ios-blue);
      background-color: rgba(0, 122, 255, 0.1);
    }

    /* Active indicator dot */
    .admin-nav-item.active::after {
      content: '';
      position: absolute;
      bottom: -8px;
      left: 50%;
      transform: translateX(-50%);
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background-color: var(--ios-blue);
    }

    /* ----- START: Comprehensive Dark Mode Styles ----- */
    
    /* Dark mode variables (should match ios-design.css or index.css) */
    @media (prefers-color-scheme: dark) {
      body.auto-theme {
        --ios-system-background: #000000;
        --ios-secondary-system-background: #1C1C1E;
        --ios-tertiary-system-background: #2C2C2E;
        --ios-label: #FFFFFF;
        --ios-secondary-label: #EBEBF599; /* ~60% opacity */
        --ios-tertiary-label: #EBEBF54D;  /* ~30% opacity */
        --ios-quaternary-label: #EBEBF52E; /* ~18% opacity */
        --ios-separator: #38383A; /* Opaque separator */
        --ios-blue: #0A84FF;
        --ios-purple: #BF5AF2;
        --ios-indigo: #5E5CE6;
        --ios-gray: #8E8E93;
      }
    }
    
    body.dark-theme {
      --ios-system-background: #000000;
      --ios-secondary-system-background: #1C1C1E;
      --ios-tertiary-system-background: #2C2C2E;
      --ios-label: #FFFFFF;
      --ios-secondary-label: #EBEBF599; /* ~60% opacity */
      --ios-tertiary-label: #EBEBF54D;  /* ~30% opacity */
      --ios-quaternary-label: #EBEBF52E; /* ~18% opacity */
      --ios-separator: #38383A; /* Opaque separator */
      --ios-blue: #0A84FF;
      --ios-purple: #BF5AF2;
      --ios-indigo: #5E5CE6;
      --ios-gray: #8E8E93;
    }

    /* General dark mode styles */
    body.dark-theme,
    body.auto-theme.dark-mode {
      background-color: var(--ios-system-background);
      color: var(--ios-label);
    }
    
    /* Dark mode for Admin Cards */
    body.dark-theme .admin-card,
    body.auto-theme.dark-mode .admin-card {
      background-color: var(--ios-secondary-system-background);
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2); /* Slightly stronger shadow */
    }
    
    body.dark-theme .admin-card-title,
    body.auto-theme.dark-mode .admin-card-title {
      color: var(--ios-label);
    }
    
    body.dark-theme .admin-card-text,
    body.auto-theme.dark-mode .admin-card-text {
      color: var(--ios-secondary-label);
    }

    /* Dark mode for Admin Nav */
    body.dark-theme .admin-nav,
    body.auto-theme.dark-mode .admin-nav {
      background: rgba(28, 28, 30, 0.95);
      backdrop-filter: blur(20px);
      -webkit-backdrop-filter: blur(20px);
      box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1);
    }
    
    body.dark-theme .admin-nav-link,
    body.auto-theme.dark-mode .admin-nav-link {
        color: var(--ios-secondary-label);
    }
    
    body.dark-theme .admin-nav-link:hover,
    body.dark-theme .admin-nav-item.active .admin-nav-link,
    body.auto-theme.dark-mode .admin-nav-link:hover,
    body.auto-theme.dark-mode .admin-nav-item.active .admin-nav-link {
      color: var(--ios-blue);
      background-color: rgba(10, 132, 255, 0.2);
    }

    /* Dark mode for theme toggle */
    .theme-toggle {
      position: fixed;
      right: 20px;
      bottom: 20px;
      width: 48px;
      height: 48px;
      border-radius: 24px;
      background-color: var(--ios-tertiary-system-background);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      z-index: 1000;
      transition: all 0.3s ease;
    }

    body.dark-theme .theme-toggle,
    body.auto-theme.dark-mode .theme-toggle {
      background-color: #3a3a3c; /* Slightly darker gray */
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .theme-toggle i {
      font-size: 22px;
      color: var(--ios-secondary-label);
      transition: color 0.3s ease;
    }

    body.dark-theme .theme-toggle i,
    body.auto-theme.dark-mode .theme-toggle i {
      color: #fff;
    }
    /* ----- END: Comprehensive Dark Mode Styles ----- */

    /* Responsive styles (Keep existing) */
    @media (max-width: 768px) {
      .admin-nav-link span {
        font-size: 12px;
      }
      
      .admin-nav-link {
        padding: 8px 12px;
      }
    }
    @keyframes fadeInUp {
      from {
        opacity: 0;
        transform: translateY(30px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    @keyframes fadeIn {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }

    @keyframes slideInCard {
      from {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
      }
      to {
        opacity: 1;
        transform: translateY(0) scale(1);
      }
    }

    /* Responsive improvements */
    @media (max-width: 768px) {
      .admin-header h1 {
        font-size: 2.2rem;
      }
      
      .admin-grid {
        grid-template-columns: 1fr;
        gap: 20px;
      }
      
      .admin-card-link {
        padding: 14px 24px;
        font-size: 0.9rem;
      }
    }
  </style>
</head>
<body class="auto-theme"> <!-- Add auto-theme class -->
<!-- 导航栏容器，将由JavaScript加载 -->
<div id="navbar-container"></div>

<!-- Admin Navigation Bar -->
<div class="admin-nav" style="display: none;">
  <div class="container">
    <ul class="admin-nav-list">
      <li class="admin-nav-item active">
        <a href="mgmt.html" class="admin-nav-link">
          <i class="fas fa-tachometer-alt"></i>
          <span>Admin Dashboard</span>
        </a>
      </li>
      <li class="admin-nav-item">
        <a href="ptmgmt.html" class="admin-nav-link">
          <i class="fas fa-clipboard-check"></i>
          <span>Carbon Review</span>
        </a>
      </li>
      <li class="admin-nav-item">
        <a href="productmgmt.html" class="admin-nav-link">
          <i class="fas fa-store"></i>
          <span>Product Management</span>
        </a>
      </li>
      <li class="admin-nav-item">
        <a href="usermgmt.html" class="admin-nav-link" id="userMgmtNav">
          <i class="fas fa-users"></i>
          <span>User Management</span>
        </a>
      </li>
      <li class="admin-nav-item">
        <a href="avatar_mgmt.html" class="admin-nav-link" id="avatarMgmtNav">
          <i class="fas fa-user-circle"></i>
          <span>Avatar Management</span>
        </a>
      </li>
       <li class="admin-nav-item">
        <a href="error_dashboard.html" class="admin-nav-link">
          <i class="fas fa-bug"></i>
          <span>Error Dashboard</span>
        </a>
      </li>
      <li class="admin-nav-item">
        <a href="index.html" class="admin-nav-link">
          <i class="fas fa-home"></i>
          <span>Return to Site</span>
        </a>
      </li>
    </ul>
  </div>
</div>

<!-- Header -->
<header class="admin-header">
  <div class="container">
    <h1>Admin Management Console</h1>
    <p>Welcome, Admin. Access various management tools below.</p>
  </div>
</header>

<!-- Management Cards -->
<div class="container" style="padding-top: 40px; padding-bottom: 60px;">
  <div class="admin-cards-grid">
    <div class="admin-card ios-hover-effect">
      <div class="admin-card-body">
        <div class="admin-card-icon"><i class="fas fa-clipboard-check"></i></div>
        <h3 class="admin-card-title">Carbon Review</h3>
        <p class="admin-card-text">Review and approve pending user carbon reduction submissions.</p>
        <a href="ptmgmt.html" class="admin-card-link">Go to Carbon Review</a>
      </div>
    </div>
    <div class="admin-card ios-hover-effect">
      <div class="admin-card-body">
        <div class="admin-card-icon"><i class="fas fa-store"></i></div>
        <h3 class="admin-card-title">Product Management</h3>
        <p class="admin-card-text">Add, edit, or remove products from the Carbon Store.</p>
        <a href="productmgmt.html" class="admin-card-link">Go to Product Management</a>
      </div>
    </div>
    <div class="admin-card ios-hover-effect">
      <div class="admin-card-body">
        <div class="admin-card-icon"><i class="fas fa-users"></i></div>
        <h3 class="admin-card-title">User Management</h3>
        <p class="admin-card-text">View, edit, or manage user accounts and their details.</p>
        <a href="usermgmt.html" class="admin-card-link">Go to User Management</a>
      </div>
    </div>
     <div class="admin-card ios-hover-effect">
      <div class="admin-card-body">
        <div class="admin-card-icon"><i class="fas fa-bug"></i></div>
        <h3 class="admin-card-title">Error Dashboard</h3>
        <p class="admin-card-text">Monitor website errors and system health.</p>
        <a href="error_dashboard.html" class="admin-card-link">Go to Error Dashboard</a>
      </div>
    </div>
  </div>
</div>

<!-- Theme toggle button placeholder (will be created by JS) -->

<!-- Footer -->
<div id="footer-placeholder"></div>

<script>
  $(document).ready(function() {
    // Check login status and admin privileges
    var token = localStorage.getItem('token');
    var isAdmin = false; // Assume not admin initially

    if (!token) {
      // Not logged in, redirect to index
      showAlert('Please log in first.', 'warning', () => { window.location.href = 'index.html'; });
      return;
    }
    
    $.ajax({
      url: 'verify_admin.php',
      type: 'POST',
      data: { token: token },
      dataType: 'json',
      async: false, // Make synchronous to ensure admin status is set before proceeding
      success: function(response) {
        if (response.success && response.isAdmin) {
          isAdmin = true;
           $('.admin-nav').show(); // Show admin nav only if admin
           // Add User Management link dynamically if needed (or ensure it's correct)
           $('#userMgmtNav').attr('href', 'usermgmt.html'); 
        } else {
          // Not an admin or token invalid, redirect
          showAlert('Admin access required.', 'error', () => { window.location.href = 'index.html'; });
        }
      },
      error: function() {
        // AJAX error, redirect
        showAlert('Failed to verify admin status.', 'error', () => { window.location.href = 'index.html'; });
      }
    });
    
    // If not admin after check, stop script execution
    if (!isAdmin) {
      return; 
    }
    
    // Function to set the active nav item based on current page
    function setActiveNavItem() {
      var currentPath = window.location.pathname;
      var filename = currentPath.substring(currentPath.lastIndexOf('/') + 1);
      
      // Remove active class from all nav items first
      $('.admin-nav-item').removeClass('active');
      
      // Add active class to the appropriate nav item
      if (filename === 'productmgmt.html') {
        $('.admin-nav-item:has(a[href="productmgmt.html"])').addClass('active');
      } else if (filename === 'usermgmt.html') {
        $('.admin-nav-item:has(a[href="usermgmt.html"])').addClass('active');
      } else if (filename === 'avatar_mgmt.html') {
        $('.admin-nav-item:has(a[href="avatar_mgmt.html"])').addClass('active');
      } else if (filename === 'error_dashboard.html') {
        $('.admin-nav-item:has(a[href="error_dashboard.html"])').addClass('active');
      } else if (filename === 'mgmt.html' || filename === '') {
         // Default to Admin Dashboard if on mgmt.html or root path within admin context
         $('.admin-nav-item:has(a[href="mgmt.html"])').addClass('active');
      }
    }
    
    // Set Active Nav Item
    setActiveNavItem();
  }); // End of document ready

    // ----- START: Theme Toggling Logic -----
    const savedTheme = localStorage.getItem('theme');
    const body = document.body;

    function applyTheme(theme) {
        body.className = theme; // Replace all classes
        updateThemeIcon(theme);
        localStorage.setItem('theme', theme);
    }

    if (savedTheme) {
        applyTheme(savedTheme);
    } else {
        body.classList.add('auto-theme'); // Start with auto
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
            body.classList.add('dark-mode');
        }
        updateThemeIcon('auto-theme'); // Update icon based on initial auto state
    }
    
    // Listen for system theme changes
    window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(e) {
      if (body.classList.contains('auto-theme')) {
        if (e.matches) {
          body.classList.add('dark-mode');
        } else {
          body.classList.remove('dark-mode');
        }
        updateThemeIcon('auto-theme'); // Update icon for auto theme change
      }
    });
    
    // Create and add theme toggle button
    const themeToggle = document.createElement('div');
    themeToggle.className = 'theme-toggle';
    themeToggle.id = 'themeToggle';
    themeToggle.innerHTML = '<i class="fas fa-moon"></i>'; // Default icon
    document.body.appendChild(themeToggle);
    updateThemeIcon(body.className || 'auto-theme'); // Set initial icon correctly

    // Add click listener for theme toggle
    themeToggle.addEventListener('click', function() {
      let nextTheme;
      if (body.classList.contains('auto-theme')) {
          nextTheme = 'dark-theme';
          body.classList.remove('dark-mode'); // Ensure dark-mode is removed when switching from auto
      } else if (body.classList.contains('dark-theme')) {
          nextTheme = 'light-theme';
      } else { // Must be light-theme
          nextTheme = 'auto-theme';
          // Re-apply dark-mode class if system prefers dark when switching back to auto
          if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
              body.classList.add('dark-mode');
          }
      }
      applyTheme(nextTheme);
    });
    
    // Function to update the theme icon
    function updateThemeIcon(theme) {
        const icon = document.querySelector('#themeToggle i');
        if (!icon) return;
        
        if (theme.includes('dark-theme')) {
          icon.className = 'fas fa-sun'; // Show sun in dark mode
        } else if (theme.includes('light-theme')) {
          icon.className = 'fas fa-moon'; // Show moon in light mode
        } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
          // Auto theme - System is dark, show sun
          icon.className = 'fas fa-sun';
        } else {
          // Auto theme - System is light, show moon
          icon.className = 'fas fa-moon';
        }
      }
      // ----- END: Theme Toggling Logic -----
</script>

</body>
</html>
